import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { config } from '../../shared/smartadmin.config';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
declare var $: any;
declare var echarts: any;
@Component({
	selector: 'sa-push-log',
	templateUrl: './push-log.component.html',
	styleUrls: ['./push-log.component.css']
})
export class PushLogComponent implements OnInit {
	tableData: any[] = []; //table数据
	totalCount: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	deviceId: string = '';//设备ID
	carNumber: string = ''; //车牌号码
	selectedCompanyId: string = '';
	selectedCompanyName: string = '';
	activeSelected: string = '';
	pushStatus: string = '';
	allChecked: boolean = false; // 全选状态
	selected: any[] = []; // 选中的数据
	selectedRow: any;

	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute,) {
		activatedRoute.queryParams.subscribe(queryParams => {

		});
	}
	//公司选中回调
	companySelected(data) {
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
		this.deviceId = data.deviceId;
		this.carNumber = data.carNumber;
	}
	ngOnInit() {
		this.load()
	}
	//删除
	delete_row(row) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(() => {
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("exceptionOrderForHaiLuo/deleteExceptionOrderById.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				this.load();
			})
		})
	}
	//  初始化加载list数据
	load() {

		this.allChecked = false; // 全选置空
		this.selected = []
		let data = {
			pageSize: this.pageSize,
			nowPage: this.curPage,
			parameters: {
				pushStatus: this.pushStatus == '' ? null : this.pushStatus,
				deviceId: this.deviceId,
				companyId: this.selectedCompanyId,
				companyName: this.selectedCompanyName,
				carNumber: this.carNumber
			}
		}
		let reqdata = 'gridPager=' + JSON.stringify(data);
		this.jsonApiService.http("HuaxinPushCarLog/page.html", reqdata).subscribe(data => {
			if (!data.isSuccess) {
				this.jsonApiService.message(data.message);
				return;
			}
			this.tableData = data.exhibitDatas;
			this.totalCount = data.recordCount;
		})
	}
	ngOnDestroy() {
		document.onclick = null;
	}
	//刷新
	refresh() {
		this.load();
	}
	//查询
	table_search() {
		this.curPage = 1;
		this.load();
	}
	//分页回调
	paginate(e) {
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}
	// 批量推送
	pushAll() {
		if (this.selected.length) {
			let reqdata = 'ids=' + this.selected.join(',');
			this.jsonApiService.http("HuaxinPushCarLog/push.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) { this.load(); }
			})
		} else {
			this.jsonApiService.message('请勾选数据推送');
		}
	}
	//单独推送
	pushItem(row) {
		let selected = this.selected.length ? this.selected : [row.id]
		let reqdata = 'ids=' + selected;
		this.jsonApiService.http("HuaxinPushCarLog/push.html", reqdata).subscribe(result => {
			this.jsonApiService.message(result.message);
			if (result.success) { this.load(); }
		});
	}
	//table复选框数据row选中事件回调:处理选中状态和deviceId
	rowSelected(row, e, index) {
		var index = $.inArray(row.id, this.selected);
		if (index === -1) {
			row.isChecked = true;
			this.selected.push(row.id);
		} else {
			row.isChecked = false;
			this.selected.splice(index, 1);
		}
		this.selected.length === this.pageSize ? this.allChecked = true : this.allChecked = false; //判断是否全选状态
	}
	//table 全部选中
	allSelected() {
		this.allChecked = !this.allChecked;
		if (!this.allChecked) {
			for (let item of this.tableData) {
				item['isChecked'] = false;
			}
			this.selected = [];
		} else {
			for (let item of this.tableData) {
				item['isChecked'] = true;
				this.selected.push(item.id);
			}
		}
	}
}
